package com.zhongkai.web.tag;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.log4j.Logger;

import com.zhongkai.dao.TagDao;
import com.zhongkai.tools.CheckWord;
import com.zhongkai.tools.SpringUtils;

public class ViewByColumnTag extends BodyTagSupport {
	Logger log = Logger.getLogger(this.getClass());
	private String table;
	private String name = "row";		
	
	@Override
	public int doEndTag() {
		try {
			if (table == null || table.matches("\\s*") ||  CheckWord.hasInvalidWord(table))
				throw new RuntimeException("对像名不存在!");				
			TagDao tagDao = (TagDao) SpringUtils.getBean("tagDao",
					this.pageContext.getServletContext());
			String queryString="from "+table;			
			if(getBodyContent().getString()!=null){
				String sqlwhere="  where "+getBodyContent().getString();
				queryString+=sqlwhere;
			}			
			List o=tagDao.select(queryString);
			if(o!=null&&o.size()>0){				
				pageContext.setAttribute(name, o.get(0));
			}else{
				pageContext.setAttribute(name, null);
			}			
			return EVAL_PAGE;
		} catch (Exception ex) {
			try {
				log.error(ex.getMessage());
				((HttpServletResponse) pageContext.getResponse())
						.sendRedirect("/errorpage.jsp?errorinfo="
								+ java.net.URLEncoder.encode(ex.toString(),"UTF-8"));
			} catch (java.io.IOException ioe) {
				log.error(ioe.getMessage());
			} 		
			return EVAL_PAGE;
		}
	}
	public String getTable() {
		return table;
	}
	public void setTable(String table) {
		this.table = table;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	
	
	
	
	
	
}
